#include<iostream>
#include "employee.h"
#include "iterator.h"
class System
{
private:
    std::string eid;
    std::string name;
    std::string gender;
    std::string dob;
    std::string doe;
    std::string unit;
    std::string title;
    int salary;
    list<employee*> mylist;
    char *readData=new char[1024];

    void insertData(){
        std::cout<<"输入数据:";
        getline(cin,eid);//抵消输入区缓存
        std::cout<<"eid:"; getline(cin,eid);if(findSpace(eid))return;
        std::cout<<"name:";getline(cin,name);if(findSpace(name))return;
        std::cout<<"gender(man,woman):";getline(cin,gender);if(findSpace(gender))return;
        std::cout<<"dob:";getline(cin,dob);if(findSpace(dob))return;
        std::cout<<"doe:";getline(cin,doe);if(findSpace(doe))return;
        std::cout<<"unit:(HumanResourcesDepartment;FinanceDepartment;SalesDepartment;AdministrationDepartment)";getline(cin,unit);if(findSpace(unit))return;
        std::cout<<"title:(Manager;HR;Financial;Sales;Executive)";getline(cin,title);if(findSpace(title))return;
        std::cout<<"salary:";std::cin>>salary;
        if(findIllegalGender(gender)||findIllegalTitle(title)||findIllegalEid(eid))
            return;
        employee* emp=new employee(eid,name,gender,dob,doe,unit,title,salary);
        insertDataIntoList(emp);
        emp->print();
        std::cout<<"   enter success!"<<std::endl;
    }
    void insertDataIntoList(employee* emp){
        mylist.push_back(emp);
    }
    bool findIllegalEid(string eid){
        if(queryDataWitheid(eid)!=NULL){
            std::cout<<"you input a illegal Eid,which means this is a Existed Eid!\n";
            return true;
        }
        return false;
    }
    bool findIllegalGender(string gender){
        if(gender.find("man")==string::npos&&gender.find("woman")==string::npos){
            std::cout<<"you input a illegal Gender!\n";
            return true;
        }
        return false;
    }
    bool findIllegalUnit(string unit){
        if(title.find("HumanResourcesDepartment")==string::npos&&title.find("FinanceDepartment")==string::npos&&title.find("SalesDepartment")==string::npos&&title.find("AdministrationDepartment")==string::npos){
            std::cout<<"you input a illegal Title!\n";
            return true;
        }
        return false;
    }
    bool findIllegalTitle(string title){
        if(title.find("Manager")==string::npos&&title.find("HR")==string::npos&&title.find("Financial")==string::npos&&title.find("Sales")==string::npos&&title.find("Executive")==string::npos){
            std::cout<<"you input a illegal Title!\n";
            return true;
        }
        return false;
    }
    bool findSpace(string input){
        if(input.find(" ")!=string::npos){
            std::cout<<"can not contain space in the data!\n";
            return true;
        }
        return false;
    }

    void insertDataWithFile(){
        string fileName;
        std::cout<<"please input the name of the file(.txt is necessery!):";
        std::cin>>fileName;
        FILE* datafile=fopen(fileName.c_str(),"r");
        while (fgets(readData,1024,datafile)!=NULL)
            if(readData[0]=='e')  
                saveDataIntoList(readData);
        fclose(datafile);
    }

    void deleteData(){
        std::string eidToDelete;
        std::cout<<"enter the eid you want to delete:";
        std::cin>>eidToDelete;
        eidToDelete="eid:"+eidToDelete;
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            std::cout<<(*it)->get_eid();
            if((*it)->get_eid().compare(eidToDelete)==0){
                mylist.remove(it);
                std::cout<<"删除成功!"<<std::endl;
                return;
            }
        }
        std::cout<<"未找到该数据!"<<std::endl;
    }
    
    void queryData(){
        std::string dataToQuery;
        int flag,times=0;
        int up,down;
        std::cout<<"please choose which data do you want to query:\n"
        <<"1.eid 2.name 3.unit 4.title 5.salary"
        <<std::endl;
        std::cin>>flag;
        switch (flag)
        {
        case 1:
            std::cout<<"please input the eid you want to query(* means all):";
            std::cin>>dataToQuery;
            queryWithEid(dataToQuery);
            break;
        case 2:
            std::cout<<"please input the name you want to query:";
            std::cin>>dataToQuery;dataToQuery="name:"+dataToQuery;
            queryWithName(dataToQuery);
            break;
        case 3:
            std::cout<<"please input the unit you want to query:";
            std::cin>>dataToQuery;dataToQuery="unit:"+dataToQuery;
            queryWithUnit(dataToQuery);
            break;
        case 4:
            std::cout<<"please input the title you want to query:";
            std::cin>>dataToQuery;dataToQuery="title:"+dataToQuery;
            queryWithTitle(dataToQuery);
            break;
        case 5:
            std::cout<<"please input the range of salary(The former is the lower limit,the latter is the largest limit):";
            std::cin>>down>>up;
            queryWithSalary(up,down);
            break;
        default:
            break;
        }
    }
    void queryWithEid(std::string eidToQuery){
        if(eidToQuery.compare("*")==0){
            list<employee*>::iterator it=mylist.begin();
            list<employee*>::iterator it2=mylist.end();
            for(;it!=it2;it++)
                (*it)->print();
        }else{
            list<employee*>::iterator it=mylist.begin();
            for(;it!=mylist.end();it++)
                if((*it)->get_eid().compare("eid:"+eidToQuery)==0)
                    (*it)->print(); 
        }
    }
    void queryWithName(std::string nameToQuery){
        list<employee*>::iterator it=mylist.begin();
        int time=0;
        for(;it!=mylist.end();it++){
            if((*it)->get_name().compare(nameToQuery)==0){
                (*it)->print();
                time++;
            }
        }
        if(time==0) std::cout<< "can't find this name" <<endl;   
    }
    void queryWithUnit(std::string unitToQuery){
        list<employee*>::iterator it=mylist.begin();
        int time=0;
        for(;it!=mylist.end();it++){
            if((*it)->get_dob().compare(unitToQuery)==0){
                (*it)->print();
            }
        }
        if(time==0) std::cout<< "can't find this unit" <<endl; 
    }
    void queryWithTitle(std::string titleToQuery){
        list<employee*>::iterator it=mylist.begin();
        int time=0;
        for(;it!=mylist.end();it++){
            if((*it)->get_title().compare(titleToQuery)==0){
                (*it)->print();
                time++;
            }
        }
        if(time==0) std::cout<< "can't find this title" <<endl; 
    }
    void queryWithSalary(int up,int down){
        list<employee*>::iterator it=mylist.begin();
        int time=0;
        for(;it!=mylist.end();it++){
            if((*it)->get_salary()<up&&(*it)->get_salary()>down){
                (*it)->print();
                time++;
            }
        }
        if(time==0) std::cout<< "can't find this salary" <<endl; 
    }
    

    employee* queryDataWitheid(std::string eidToQuery){
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            if((*it)->get_eid().compare("eid:"+eidToQuery)==0){  
                return *it;
            }  
        } 
        return NULL;
    }

    void changeData(){
        int flag=0;
        std::cout<<"select which information do you want to change:\n"
        <<"1 means change people's information\n"
        <<"2 means change money information\n";
        std::cin>>flag;
        switch (flag)
        {
        case 1:
            changeEmployInformation();
            break;
        default:
            changeSalary();
            break;
        }
    }
    void changeEmployInformation(){
        std::string flag;
        std::string eidToChange;
        std::cout<<"input eid:";
        std::cin>>eidToChange;
        eidToChange=eidToChange;
        employee* result=queryDataWitheid(eidToChange);
        if(result!=NULL){
            getchar();
            std::cout<<(*result).get_name()<<",do you want to change(0 means yes):";std::cin>>flag;
            if(flag.compare("0")==0){(*result).set_name("name:"+setNewData());}
            std::cout<<(*result).get_gender()<<",do you want to change(0 means yes):";std::cin>>flag;
            if(flag.compare("0")==0){(*result).set_gender("gender:"+setNewData());}
            std::cout<<(*result).get_dob()<<",do you want to change(0 means yes):";std::cin>>flag;
            if(flag.compare("0")==0){(*result).set_dob("dob:"+setNewData());}
            std::cout<<(*result).get_doe()<<",do you want to change(0 means yes):";std::cin>>flag;
            if(flag.compare("0")==0){(*result).set_doe("doe:"+setNewData());}
            std::cout<<(*result).get_unit()<<",do you want to change(0 means yes):";std::cin>>flag;
            if(flag.compare("0")==0){(*result).set_unit("unit:"+setNewData());}
            std::cout<<(*result).get_title()<<",do you want to change(0 means yes):";std::cin>>flag;
            if(flag.compare("0")==0){(*result).set_title(setNewData());}
        }else{
            std::cout<<"未查询到这条数据";
        }
    }
    string setNewData(){
        string returnString;
        std::cout<<"enter new data:";
        std::cin>>returnString;
        return returnString;
    }
    void changeSalary(){
        int newSalary;
        string title;
        std::cout<<"input the title:";
        std::cin>>title;
        std::cout<<"input new salary:";
        std::cin>>newSalary;
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            if((*it)->get_title().compare("title:"+title)==0){
                (*it)->set_salary(newSalary);
                return;
            }
        }
    }

    void staticsData(){
        int flag=0;
        std::cout<<"select which information do you want to statics:\n"
        <<"1 means statics gender\n"
        <<"2 means statics unit\n"
        <<"3 means statics title\n";
        std::cin>>flag;
        switch (flag)
        {
        case 1:
            staticsGender();
            break;
        case 2:
            staticsUnit();
            break;
        default:
            staticsTitle();
            break;
        }
    }
    void staticsGender(){
        int times=0;
        std::string genderToStatics;
        std::cout<<"input which gender do you want to statics:";
        std::cin>>genderToStatics;
        genderToStatics="gender:"+genderToStatics;
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            if((*it)->get_gender().compare(genderToStatics)==0){
                (*it)->print();
                times++;
            }
        }
        std::cout<<"total results:"<<times<<std::endl;
    }
    void staticsUnit(){
        int times=0;
        std::string unitToStatics;
        std::cout<<"input which unit do you want to statics:";
        std::cin>>unitToStatics;
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            if((*it)->get_unit().compare(unitToStatics)==0){
                (*it)->print();
                times++;
            }
        }
        std::cout<<"total results:"<<times<<std::endl;
    }
    void staticsTitle(){
        string title;
        int time=0;
        std::cout<<"enter the title you want to query:";
        std::cin>>title;
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            if((*it)->get_title().compare("title:"+title)==0){
                (*it)->print();
                time++;
            }
        }
        std::cout<<title<<" has "<<time<<" results.\n";
    }

    void saveDataIntoList(char *pBuff){
        employee *emp=new employee(pBuff);
        mylist.push_back(emp);
    }
    void insertDataIntoFile(employee* emp){
        FILE* datafile= NULL;
        datafile=fopen("dada.txt","a");
        fprintf(datafile,"%s %s %s %s %s %s %s salary:%d\n"
        ,emp->get_eid().c_str()
        ,emp->get_name().c_str()
        ,emp->get_gender().c_str()
        ,emp->get_dob().c_str()
        ,emp->get_doe().c_str()
        ,emp->get_unit().c_str()
        ,emp->get_title().c_str()
        ,emp->get_salary()
        );
        fclose(datafile);
    }

    void printList(){
        for(list<employee*>::iterator it=mylist.begin();it!=mylist.end();it++){
            (*it)->print();
            std::cout<<std::endl;
        }
    }
public:
    System(){

    }
    void printUserInterface(){
        int flag=0,i=0;
        std::cout<<"This is a Employee Manage System!\n"<<std::endl;
        while (i==0)
        {
            std::cout<<"-------------------menu---------------------"<<std::endl;
            std::cout<<"1.Insert data                  2.delete data"<<std::endl;
            std::cout<<"3.query data                   4.change data"<<std::endl;
            std::cout<<"5.statistic data               6.Insert data with file"<<std::endl;
            std::cout<<"choose operation:";
            std::cin>>flag;
        switch (flag)
        {
        case 1:
            insertData();break;
        case 2:
            deleteData();break;
        case 3:
            queryData();break;
        case 4:
            changeData();break;
        case 5:
            staticsData();break;
        default:
            break;
        }
        std::cout<<"enter 0 to continue:";
        std::cin>>i;
        }
    }

    void readAndSaveDataFromFile(){
        FILE* datafile= NULL;
        int nReadByte = 0;
        if((datafile=fopen("dada.txt","r"))==NULL)
        datafile=fopen("dada.txt","a");
        while (fgets(readData,1024,datafile)!=NULL)
            if(readData[0]=='e')  
                saveDataIntoList(readData);
        datafile=fopen("dada.txt","w+");
        fclose(datafile);
    }

    void sortList(){
        mylist.sort();
    }

    void saveListsIntoFile(){
        list<employee*>::iterator it=mylist.begin();
        for(;it!=mylist.end();it++){
            insertDataIntoFile(*it);
        }
        std::cout<<"最终数据为:\n";
        printList();
    }
};

